<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Model\AppChannel;
use App\Model\AppVersion;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;

class ChannelController extends Controller
{
    use ModelForm;

    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('渠道号管理');
            $content->description('');
            $content->body($this->grid());
        });
    }

    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('编辑');
            $content->body($this->form()->edit($id));
        });
    }

    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('添加');
            $content->description('');
            $content->body($this->form());
        });
    }

    protected function grid()
    {
        return Admin::grid(AppChannel::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->platform('平台')->display(function ($data) {
                return $data == 1 ? '苹果' : '安卓';
            });
            $grid->package_name('包名');
            $grid->channel('渠道号');
            $grid->channel_name('渠道号备注');
            // $grid->apk_name('APK包');
            // $grid->app_version('应用版本号');
            // $grid->download_link('下载链接')->display(function ($data) {
            //     return $data  ? AliyunUtil::FILE_HOST . $data : '禁用';
            // });
            // $grid->reg_num('注册人数')->display(function ($data) {
            //     return User::getChannelNum($this->channel);
            // });
            // $grid->stat_link_click('链接点击次数');
            // $grid->stat_link_download('链接下载次数');

            $grid->flag('使用状态')->display(function ($data) {
                return $data == 1 ? '启用' : '禁用';
            });
            $grid->admin_id('操作人')->display(function ($data) {
                return $data;
            });
            //禁用导出
            $grid->disableExport();
            //禁用行选择
            $grid->disableRowSelector();
            // $grid->disableActions();
            $grid->filter(function ($filter) {
                $filter->expand();
                $filter->disableIdFilter();
                $filter->column(1 / 3, function ($filter) {
                    $filter->equal('platform', '平台')->select(AppVersion::PLATFORM);
                });
                $filter->column(1 / 3, function ($filter) {
                    $filter->equal('flag', '使用状态')->select([-1 => '全部', 0 => '禁用', 1 => '启用']);
                });
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->disableView();
            });
        });
    }

    protected function form($mode = '', $id = '')
    {
        return Admin::form(AppChannel::class, function (Form $form) use ($mode, $id) {
            $form->select('platform', '平台')->options(AppVersion::PLATFORM);
            $form->text('package_name', '包名')->default('');
            $form->text('channel', '渠道号');
            $form->text('channel_name', '渠道号备注');
            $form->text('app_version', '应用版本号');
            $form->file('download_link', '上传APK包');
            $form->select('flag', '是否开启')->options([0 => '禁用', 1 => '启用']);
        });
    }
}
